#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int fd[2];
    pipe(fd);
    int n;

    pid_t pid = fork();

    if (!pid) {
        close(fd[1]);
        char buff[128] = {0};

        n = 0;
        while ((n = read(fd[0], buff, 127)) > 0) {
            printf("child read:%s\n", buff);
        }
        close(fd[0]);
    } else {
        close(fd[0]);
        while (1) {
            char buff[128] = {0};
            printf("input:\n");
            fgets(buff, 128, stdin);
            if (!strncmp(buff, "end", 3)) {
                break;
            }
            write(fd[1], buff, 127);
        }
        close(fd[1]);
    }

    exit(0);
}